<?php
    require_once 'header.php';
   	require_once 'config.php';
	require_once 'Date.php';

    if(!isset($_REQUEST['type']) || empty($_REQUEST['type'])) {
        $_REQUEST['type'] = "cat";
    }

    $type = $_REQUEST['type'];

    if(!empty($_REQUEST["month"])) {
	    $m = (int)$_REQUEST["month"];
    }
    else {
	    $m = date('m');
    }

	$m = sprintf("%02d", $m);

	if(!empty($_REQUEST["year"])) {
	    $y = (int)$_REQUEST["year"];
    }
    else {
        $y = date('Y');
    }

	if(!empty($_REQUEST["date"])) {
	    $d = (int)$_REQUEST["date"];
    }
    else {
        $d = date('d');
    }

    $d = sprintf("%02d", $d);

	if(!isset($_REQUEST['duration'])) {
		$_REQUEST['duration'] = "monthly";
	}

	$cats = getCategory();

	$catexp = array();

	if($_REQUEST['duration'] == 'monthly') {
	    $sum = getTotalExpenseForMonth($y, $m, $_SESSION['userid']);
		if(is_array($cats)) {
			foreach($cats as $cat) {
		    	$catexp[$cat['category_name']] = array();
			    $catexp[$cat['category_name']][0] = getCategoryExpenseForMonth($y, $m, $cat['category_id'], $_SESSION['userid']);
			    $catexp[$cat['category_name']][1] = $cat['category_color'];
	        }
        }
    var_dump($catexp);
?>
        <h4>Total Expenses by Category for the month of <?= $Months[(int)$m] ?>, <?= $y ?></h4>
<?php
    }
    else if($_REQUEST['duration'] == 'weekly') {
        $sum = getWeekData($m, $d, $y, $_SESSION['userid']);
        if(is_array($cats)) {
            foreach($cats as $cat) {
                $catexp[$cat['category_name']] = array();
                $catexp[$cat['category_name']][0] = getWeekData($m, $d, $y, $_SESSION['userid'], $cat['category_id']);
                $catexp[$cat['category_name']][1] = $cat['category_color'];
            }
        }
?>
        <h4>Total Expenses by Category for the week of <?= $Months[(int)$m] ?> <?= $d ?>, <?= $y ?></h4>
<?php
    }
    else if($_REQUEST["duration"] == "yearly") {
        $sum = getYearData($y, $_SESSION['userid']);
        if(is_array($cats)) {
            foreach($cats as $cat) {
                $catexp[$cat['category_name']] = array();
                $catexp[$cat['category_name']][0] = getYearData($y, $_SESSION['userid'], $cat['category_id']);
                $catexp[$cat['category_name']][1] = $cat['category_color'];
            }
        }
?>
        <h4>Total Expenses by Category for the Year <?= $y ?></h4>
<?php
    }
    $catexp['Total'] = array();
    $catexp['Total'][0] = $sum;
    $catexp['Total'][1] = 'darkred';
    //print_r($catexp);
?>
<div style="text-align: center;">
<?php
	foreach($catexp as $cat => $cexp) {
		$orig_perc = sprintf("%.2f", (100.0 * (float)$cexp[0] / (float)$sum));
        $perc = (int)(400.0 * (float)$cexp[0] / (float)$sum);
        if($cat == 'Total') {
            $perc = 400;
        }

?>
<table align="center" style="width: 600px; text-align: left; padding: 2px;" cellspacing="0" cellpadding="0">
	<tr>
    <td style="width: 200px; border: none; color: <?= $cexp[1] ?>; padding: 0px;"><b><?= $cat ?></b><br/> <?= sprintf("$%.2f", $cexp[0]) ?> (<?= $orig_perc ?>%)</td>
<?php
		if($perc > 0) {
?>
		<td class="bar" style="padding: 0px; width: <?= $perc ?>px; background-color: <?= $cexp[1] ?>;">&nbsp;</td>
<?php
        }
        if($perc != 400) {
?>
    <td style="width: <?= 400-$perc ?>px; border: 1px solid #cccccc; padding: 0px;">&nbsp;</td>
<?php
        }
?>
	</tr>
</table>
<?php
	}
?>
</div>
<p align="center">
<a href="chart.php?type=<?= $type ?>&duration=weekly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Week">This Week</a>
|
<a href="chart.php?type=<?= $type ?>&duration=monthly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Month">This Month</a>
|
<a href="chart.php?type=<?= $type ?>&duration=yearly&year=<?= $y ?>&month=<?= $m ?>&date=<?= $d ?>" title="Expenses by Category for This Year">This Year</a>
</p>
</body>
</html>
